
/*  To enable NT's Reboot feature enable the below
 *  manifest constants. By default the program will
 *  log the user off only.
 */

#define REBOOT

#define WIN32S  0x80000000l            // no manifest constance yet???

#define STRICT
#include <windows.h>                  /* required for all Windows applications */
#include <stdio.h>
#include <tchar.h>
#include "resource.h"

extern HINSTANCE hInst;

int PASCAL Reboot( HWND hWnd )
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    int x = 0;
    TCHAR szBuf[100];
    TCHAR titlebuf[256], msgbuf[256];

    LoadString(hInst, IDS_PRG_CLOSE, msgbuf, sizeof(msgbuf));
    LoadString(hInst, IDS_TITLE_CLOSE, titlebuf, sizeof(titlebuf));
    x = MessageBox(hWnd,msgbuf, titlebuf,MB_YESNO);

    if (x == IDNO) return(0);


    // Get a token for this process.
    if (! (GetVersion() & WIN32S))
        {
        OutputDebugString(TEXT("Setting token"));
        // Running on NT so need to change privileges
        if (!OpenProcessToken(GetCurrentProcess(),
                    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, & hToken))
            {
            _stprintf_s(szBuf, 100, TEXT("OpenProcessToken Error #%d"), GetLastError ());
            MessageBox(hWnd, szBuf, NULL, MB_OK);
            }

        // Get the LUID for shutdown privilege
        LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"),
            &tkp.Privileges[0].Luid);
        tkp.PrivilegeCount = 1;        // one privilege to set
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

        // Get shutdown privilege for this process.
        if (! AdjustTokenPrivileges(hToken, FALSE, & tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0))
            {
            _stprintf_s(szBuf, 100, TEXT("AdjustTokenPrivileges Error #%d"), GetLastError ());
            MessageBox(hWnd, szBuf, NULL, MB_OK);
            }
        }

#ifdef REBOOT
        // Shut down the system, and reboot the system.
    if (!ExitWindowsEx( EWX_REBOOT, 0 )) {
                _stprintf_s(szBuf, 100,TEXT("Error ExitWindows, Error #%d"), GetLastError ());
        MessageBox(hWnd, szBuf, NULL, MB_OK);
    }
#else
	// Shut down the system, and force all applications closed.
	// if (! ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))

	// Shut down the system, and close all applications (prompt for save
	// changes).
	if (! ExitWindowsEx(EWX_SHUTDOWN, 0))
	{
		_stprintf_s(szBuf, TEXT("Error ExitWindows, Error #%d"), GetLastError ());
		MessageBox(hWnd, szBuf, NULL, MB_OK);
	}
#endif
	return (0);
}


